﻿using System;
using System.Collections.Generic;
using System.Text;
using InfoJetSoft.Service.Entity;
using System.Xml;
using System.Xml.Schema;

//CID 0500 //2010.07.29 //Begin
namespace InfoJetSoft.Service.Util
{
    public class ParsedPSVI
    {
        private string _id = null;
        private string _type = null;
        private string _pattern = null;
        private string _enumeration = null;
        private string _length = null;
        private string _minLength = null;
        private string _maxLength = null;
        private string _minInclusive = null;
        private string _maxInclusive = null;
        private string _minExclusive = null;
        private string _maxExclusive = null;
        private string _fractionDigits = null;
        private string _totalDigits = null;
        private string _whiteSpace = null;
        private string _nillable = null;
        private bool _psviProcessed = false;
        private bool _nillableProcessed = false;

        private ParsedPSVI(string id)
        {
            this._id = id;
        }

        public string ID
        {
            get
            {
                return this._id;
            }
        }

        public string Type
        {
            get
            {
                return this._type;
            }
            set
            {
                this._type = value;
            }
        }

        public string Pattern
        {
            get
            {
                return this._pattern;
            }
            set
            {
                this._pattern = value;
            }
        }

        public string Enumeration
        {
            get
            {
                return this._enumeration;
            }
            set
            {
                this._enumeration = value;
            }
        }

        public string Length
        {
            get
            {
                return this._length;
            }
            set
            {
                this._length = value;
            }
        }

        public string MinLength
        {
            get
            {
                return this._minLength;
            }
            set
            {
                this._minLength = value;
            }
        }

        public string MaxLength
        {
            get
            {
                return this._maxLength;
            }
            set
            {
                this._maxLength = value;
            }
        }

        public string MinInclusive
        {
            get { return _minInclusive; }
            set { _minInclusive = value; }
        }

        public string MaxInclusive
        {
            get { return _maxInclusive; }
            set { _maxInclusive = value; }
        }

        public string MinExclusive
        {
            get { return _minExclusive; }
            set { _minExclusive = value; }
        }

        public string MaxExclusive
        {
            get { return _maxExclusive; }
            set { _maxExclusive = value; }
        }

        public string FractionDigits
        {
            get { return _fractionDigits; }
            set { _fractionDigits = value; }
        }

        public string TotalDigits
        {
            get { return _totalDigits; }
            set { _totalDigits = value; }
        }

        public string WhiteSpace
        {
            get { return _whiteSpace; }
            set { _whiteSpace = value; }
        }

        public string Nillabe
        {
            get
            {
                return this._nillable;
            }
            set
            {
                this._nillable = value;
            }
        }

        public bool PSVIProcessed
        {
            get
            {
                return this._psviProcessed;
            }
            set
            {
                this._psviProcessed = value;
            }
        }

        public bool NillableProcessed
        {
            get
            {
                return this._nillableProcessed;
            }
            set
            {
                this._nillableProcessed = value;
            }
        }

        public bool NillableBooleanValue
        {
            get
            {
                if (this.Nillabe == null)
                {
                    throw new InvalidOperationException( "ParsedPSVI " + this.ID + ": Nillable is null.");
                }
                if (this.Nillabe.Equals("true"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string NillableStringValue
        {
            get
            {
                if (this.Nillabe == null)
                {
                    return "unknown";
                }
                return this.Nillabe;
            }
        }

        //调用GetParsedPSVI后一定要判断是不是null,得到null也是正常的(没有SchemaSet).
        public static ParsedPSVI GetParsedPSVI(Form form, XmlNode node)
        {
            if (SchemaPSVI.ensurePSVI(form, node))
            {
                XmlSchemaObject key = null;
                if (node.SchemaInfo.SchemaElement != null)
                {
                    key = node.SchemaInfo.SchemaElement;
                }
                else
                {
                    key = node.SchemaInfo.SchemaAttribute;
                }
                //CID 0838 //2011.03.22 //Begin
                if (key == null)
                {
                    key = node.SchemaInfo.SchemaType;
                }
                //CID 0838 //2011.03.22 //End
                ParsedPSVI parsedPSVI = form.PSVITable[key] as ParsedPSVI;
                if (parsedPSVI == null)
                {
                    parsedPSVI = new ParsedPSVI(form.PSVITable.Count.ToString());
                    form.PSVITable[key] = parsedPSVI;
                }
                return parsedPSVI;
            }
            return null;
        }

        //CID 0757 //2011.01.12 //private
        private static void SetFullParsedPSVI(Form form, XmlNode node, ParsedPSVI parsedPSVI)
        {
            if (!parsedPSVI.NillableProcessed)
            {
                parsedPSVI.Nillabe = SchemaPSVI.isNillabe(form, node);
                parsedPSVI.NillableProcessed = true;
            }
            if (!parsedPSVI.PSVIProcessed)
            {
                SchemaPSVI.fillPSVI(form, node, parsedPSVI);
                parsedPSVI.PSVIProcessed = true;
            }
        }

        //CID 0757 //2011.01.12 //Begin
        public static ParsedPSVI GetFullParsedPSVI(Form form, XmlNode node)
        {
            ParsedPSVI parsedPSVI = ParsedPSVI.GetParsedPSVI(form, node);
            if (parsedPSVI != null)
            {
                if (!parsedPSVI.PSVIProcessed)
                {
                    ParsedPSVI.SetFullParsedPSVI(form, node, parsedPSVI);
                }
                return parsedPSVI;
            }
            return null;
        }
        //CID 0757 //2011.01.12 //End

        public static string GetSchemaType(Form form, XmlNode node)
        {
            //CID 0757 //2011.01.12 //Begin
            ParsedPSVI parsedPSVI = ParsedPSVI.GetFullParsedPSVI(form, node);
            //CID 0757 //2011.01.12 //End
            if (parsedPSVI != null)
            {
                return parsedPSVI.Type;
            }
            return null;
        }
    }
}
//CID 0500 //2010.07.29 //End
